#!/bin/bash

libraryname=$1
cellname=$2

#### run path
run_path="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/verify/check_dm"
virtuoso_run_path="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/cadence"

#### rule path
dummy_rule="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/verify/check_dm/rule/Dummy_CalibreYE_6nm.11a.forBlock"
#dummy_rule="/pdks/tsmc/CLN06FF/techfile/Calibre/Dummy_BEOL/v1.1a/Dummy_BEOL_CalibreYE_6nm_001.11a_forBlock"
#drc_rule="/projects/analog_ip/workspace/hzou/cl/tacoma6/verify/drc/CLN6FF_13M_1X1Xa1Ya5Y2Yy2R_001.10_3a.encrypt"
drc_rule="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/verify/check_dm/rule/CLN6FF_13M_1X1Xa1Ya5Y2Yy2Z_001.11a.encrypt_forIP"


###### check run path
if [[ -d "${run_path}/run_DRC" ]]; then
	echo "${run_path}/run_DRC is exists."
else
	echo "${run_path}/run_DRC not exist."
        mkdir run_DRC
fi

if [[ -d "${run_path}/run_DUM" ]]; then
	echo "${run_path}/run_DUM is exists."
else
	echo "${run_path}/run_DUM not exist."
        mkdir run_DUM
fi

if [[ -d "${run_path}/GDS" ]]; then
	echo "${run_path}/GDS is exists."
else
	echo "${run_path}/GDS not exist."
        mkdir GDS
fi


###### out GDS file

gds_strmfile="${run_path}/GDS/${cellname}.gds"
strmout_log="${run_path}/GDS/strmOut.log"

cd ${virtuoso_run_path}

strmout -library $libraryname \
        -strmFile ${gds_strmfile} \
        -topCell $cellname \
        -view layout \
        -logFile ${strmout_log} \
        -layerMap /projects/analog_ip/workspace/hzou/cl/tacoma6/cadence/layer.map \
        -enableColoring \
        -convertPcellPin geometry | tee ./strmout.log

echo "GDS STRMOUT SUCCESS"

###### run_DUM
cd ${run_path}
cd ./run_DUM

out_dum_file="${run_path}/run_DUM/F11a_${cellname}.gds"

echo "LAYOUT SYSTEM GDSII" > ./DRC_DUM.cal
echo "LAYOUT PATH '${gds_strmfile}'" >> ./DRC_DUM.cal
echo "LAYOUT PRIMARY '${cellname}'" >> ./DRC_DUM.cal
echo "INCLUDE ${dummy_rule}" >> ./DRC_DUM.cal

calibre -64 -drc -hier -hyper -turbo ./DRC_DUM.cal | tee ./run_DUM.log

mv ./FEOL.gds ${out_dum_file}

###### run DRC
cd ${run_path}
cd ./run_DRC

out_drc_file="${run_path}/run_DRC/${cellname}.gds"

#drvReplaceCell ${gds_file} ${out_dum_file} ${out_drc_file}
exc="'/users/lshi/flow/pv/merge.tcl ${gds_strmfile} ${cellname} ${out_drc_file} F11a_${cellname} ${out_dum_file}'"
echo ${exc}
eval calibredrv ${exc} | tee merge.log

echo "LAYOUT SYSTEM OASIS" > ./DRC.cal
echo "LAYOUT PATH '${out_drc_file}'" >> ./DRC.cal
echo "LAYOUT PRIMARY ${cellname}" >> ./DRC.cal
echo "DRC MAXIMUM RESULTS ALL" >> ./DRC.cal
echo "INCLUDE ${drc_rule}" >> ./DRC.cal
echo "EXCLUDE CELL DDR_WRITE_READ_H CM_LOGIC_H DDR_DQS_TRACKING DDR_STR_GATE DLL_DDR_V" >> ./DRC.cal

/apps/mentor/Calibre_2019.3_25.15/aoi_cal_2019.3_25.15/bin/calibre -drc -hier -hyper -turbo 32 ./DRC.cal | tee DRC.log


cd ${run_path}/run_DRC
rep_drc
cd ${run_path}